<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

$aCamForm = array();
require("_inc/const.campiGrut.php");

if (reqVar('azione') == 'salva') {
	$msgPag = conferma($aCamForm);
} 

$sqlSel = "SELECT u.* FROM ovf_gruppi u WHERE u.id_gruppo = ".reqVarInt('id');
$objDB->exec($sqlSel);
if(!$aRow = $objDB->fetch()) {
	backOnErr("id. errato");
}
reqDef('cod_gruppo',$aRow['cod_gruppo']);
reqDef('descr',$aRow['descr']);

$sqlSel = "SELECT * FROM ovf_gruppi_ricors ";
$objDB->exec($sqlSel);
$aGr = array();
while($acX = $objDB->fetch()) {
	$aGr[$acX['id_gruppo_dest']][$acX['id_gruppo_orig']] = 0;
}
foreach($aGr as $gDe => $aX) {
	foreach($aX as $gOr => $gOr2) {
		if (isset($aGr[$gOr])) {
			foreach($aGr[$gOr] as $gOrR => $gOrR2) {
				if (!isset($aGr[$gDe][$gOrR])) {
					$aGr[$gDe][$gOrR] = $gOr;
				}
			}
		}
	}	
}
//dbg_($aGr);
$aIdGr = array();
if (isset($aGr[reqVarInt('id')])) {
	$aIdGr = array_keys($aGr[reqVarInt('id')]);
}
$aIdGr[] = reqVarInt('id');

$sqlSel = "SELECT u.*,ugu.id_gruppo FROM ovf_utenti u "
	." INNER JOIN ovf_utenti_gruppi ugu ON ugu.id_utente = u.id_utente "
	."    AND ugu.id_gruppo IN (".implode(',',$aIdGr).") "
	." ORDER BY u.cod_utente";
$objDB->exec($sqlSel,'utentiGr');

$sqlSel = "SELECT g.* FROM ovf_gruppi g ";
$objDB->exec($sqlSel,'gruppiGr');
$aGrDes = array();
while($acX = $objDB->fetch('gruppiGr')) {
	$aGrDes[$acX['id_gruppo']]['cod'] = $acX['cod_gruppo'];
	$aGrDes[$acX['id_gruppo']]['des'] = $acX['descr'];
}

$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"grutMod.php?id=".reqVarInt('id')
	."&xsn=".XSN.";\" >modifica</a>";
$sCmdR .= cmdTorna('torna');

testaHtml('Gestione gruppi utenti','Modifica gruppo utenti',$msgPag,'gest');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Gruppo utenti</td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm,'vis');
?>
</table>
<?php 
if (isset($aGr[reqVarInt('id')])) {
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="lab1" align="center" colspan='2'>Gruppi inclusi</td>
      <td class="lab1" align="center" colspan='2'>Provenienza</td>
   </tr>
<?php 
	foreach($aGr[reqVarInt('id')] as $idGr => $idGr2) {
?>
   <tr>
      <td class="txt1" align="left" width='10%'>
      	<?php echo $aGrDes[$idGr]['cod'];?>
      </td>
      <td class="txt1" align="left" width='40%'>
      	<?php echo $aGrDes[$idGr]['des'];?>
      </td>
      <td class="txt1" align="left" width='10%'>
      	<?php 
      	echo ($idGr2 > 0) ?  $aGrDes[$idGr]['cod'] : "&nbsp;";
      	?>
      </td>
      <td class="txt1" align="left" width='40%'>
      	<?php 
      	echo ($idGr2 > 0) ?  $aGrDes[$idGr]['des'] : "&nbsp;";
      	?>
      </td>
   </tr>
<?php 
	}
?>
</table>
<?php 
}
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="lab1" align="center" colspan='2'>Utenti</td>
      <td class="lab1" align="center" colspan='2'>Provenienza</td>
   </tr>
<?php 
while($aRow = $objDB->fetch('utentiGr')) {
?>
   <tr>
      <td class="txt1" align="left" width='10%'>
      	<?php echo $aRow['cod_utente'];?>
      </td>
      <td class="txt1" align="left" width='40%'>
      	<?php echo $aRow['cognome']." ".$aRow['nome'];?>
      </td>
      <td class="txt1" align="left" width='10%'>
      	<?php 
      	echo ($aRow['id_gruppo'] != reqVarInt('id')) ?  $aGrDes[$aRow['id_gruppo']]['cod'] : "&nbsp;";
      	?>
      </td>
      <td class="txt1" align="left" width='40%'>
      	<?php 
      	echo ($aRow['id_gruppo'] != reqVarInt('id')) ?  $aGrDes[$aRow['id_gruppo']]['des'] : "&nbsp;";
      	?>
      </td>
   </tr>
<?php 
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
codaHtml();
exit();
